<template>
    <view class="content">
        <view class="input-group">
			<view class="input-row border">
			    <text class="title">姓名：</text>
			    <m-input type="text" focus clearable v-model="memberName" placeholder="请输入姓名"></m-input>
			</view>
            <view class="input-row border">
                <text class="title">手机：</text>
                <m-input type="text" focus clearable v-model="memberPhone" placeholder="请输入手机号"></m-input>
            </view>
            <view class="input-row border">
                <text class="title">密码：</text>
                <m-input type="password" displayable v-model="memberPassword" placeholder="请输入密码"></m-input>
            </view>
            <view class="input-row">
                <text class="title">邮箱：</text>
                <m-input type="text" clearable v-model="memberEmail" placeholder="请输入邮箱"></m-input>
            </view>
        </view>
        <view class="btn-row">
            <button type="primary" class="primary" @tap="register">注册</button>
        </view>
    </view>
</template>

<script>
    import service from '../../service.js';
    import mInput from '../../components/m-input.vue';

    export default {
        components: {
            mInput
        },
        data() {
            return {
                memberName: '',
                memberPhone: '',
				memberPassword: '',
                memberEmail: ''
            }
        },
        methods: {
            register() {
                /**
                 * 客户端对账号信息进行一些必要的校验。
                 * 实际开发中，根据业务需要进行处理，这里仅做示例。
                 */
				if(this.memberName == null || this.memberName == ''){
					uni.showToast({
					    icon: 'none',
					    title: '姓名不能为空'
					});
					return;
				}
				if(this.memberPhone == null || this.memberPhone == ''){
					uni.showToast({
					    icon: 'none',
					    title: '手机号不能为空'
					});
					return;
				}
                if (this.memberPassword.length < 6 || this.memberPassword.length > 18) {
                    uni.showToast({
                        icon: 'none',
                        title: '密码在6-18个字符之间'
                    });
                    return;
                }
				if(this.memberEmail != null && this.memberEmail != ''){
					if (this.memberEmail.length < 3 || !~this.memberEmail.indexOf('@')) {
						uni.showToast({
							icon: 'none',
							title: '邮箱地址不合法'
						});
						return;
					}
				}

                const data = {
                    memberName: this.memberName,
                    memberPhone: this.memberPhone,
                    memberPassword: this.memberPassword,
					memberEmail: this.memberEmail
                }
				
				uni.request({
					url: 'http://localhost:8000/member/center/register',
					method: 'POST',
					data: data,
					success: res => {
						const resData = res.data;
						if(resData.code == 200){
							uni.showToast({
							    title: '注册成功'
							});
							uni.navigateBack({
							    delta: 1
							});
						}else{
							uni.showToast({
							    icon: 'none',
							    title: resData.msg,
							});
						}
					},
					fail: () => {},
					complete: () => {}
				});                
            }
        }
    }
</script>

<style>

</style>
